package com.simon.jkxj.ui.diagnostic

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.chip.Chip
import com.simon.jkxj.R
import com.simon.jkxj.databinding.ActivityRespiratoryDiagnosisBinding
import com.simon.jkxj.model.RespiratoryDiseases
import com.simon.jkxj.model.RespiratoryDiagnosis

class RespiratoryDiagnosisActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRespiratoryDiagnosisBinding
    private val selectedSymptoms = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRespiratoryDiagnosisBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupToolbar()
        setupChipGroup()
        setupSubmitButton()
    }

    private fun setupToolbar() {
        binding.toolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }

    private fun setupChipGroup() {
        // 为每个Chip单独设置点击监听器
        binding.chipCough.setOnCheckedChangeListener { _, isChecked ->
            updateSelectedSymptoms("咳嗽", isChecked)
        }
        binding.chipPhlegm.setOnCheckedChangeListener { _, isChecked ->
            updateSelectedSymptoms("咳痰", isChecked)
        }
        binding.chipChestTightness.setOnCheckedChangeListener { _, isChecked ->
            updateSelectedSymptoms("胸闷", isChecked)
        }
        binding.chipShortnessBreath.setOnCheckedChangeListener { _, isChecked ->
            updateSelectedSymptoms("气短", isChecked)
        }
        binding.chipWheezing.setOnCheckedChangeListener { _, isChecked ->
            updateSelectedSymptoms("喘息", isChecked)
        }
    }

    private fun updateSelectedSymptoms(symptom: String, isChecked: Boolean) {
        if (isChecked) {
            if (!selectedSymptoms.contains(symptom)) {
                selectedSymptoms.add(symptom)
            }
        } else {
            selectedSymptoms.remove(symptom)
        }
        Log.d("RespiratoryDiagnosis", "Selected symptoms: $selectedSymptoms")
    }

    private fun setupSubmitButton() {
        binding.btnSubmit.setOnClickListener {
            if (validateInput()) {
                val diagnosis = createDiagnosis()
                saveDiagnosis(diagnosis)
                showDiagnosisResult(diagnosis)
            }
        }
    }

    private fun validateInput(): Boolean {
        Log.d("RespiratoryDiagnosis", "Validating input. Selected symptoms: $selectedSymptoms")
        if (selectedSymptoms.isEmpty()) {
            Toast.makeText(this, "请至少选择一个症状", Toast.LENGTH_SHORT).show()
            return false
        }

        val selectedDurationId = binding.rgDuration.checkedRadioButtonId
        if (selectedDurationId == -1) {
            Toast.makeText(this, "请选择症状持续时间", Toast.LENGTH_SHORT).show()
            return false
        }

        return true
    }

    private fun createDiagnosis(): RespiratoryDiagnosis {
        // 根据症状组合判断疾病类型
        val diseaseType = when {
            selectedSymptoms.contains("咳嗽") && selectedSymptoms.contains("咳痰") -> "支气管炎"
            selectedSymptoms.contains("胸闷") && selectedSymptoms.contains("气短") -> "哮喘"
            selectedSymptoms.contains("喘息") -> "哮喘"
            selectedSymptoms.contains("咳嗽") && selectedSymptoms.contains("气短") -> "肺炎"
            else -> "上呼吸道感染"
        }

        // 根据症状数量判断严重程度
        val severity = when (selectedSymptoms.size) {
            1 -> "轻度"
            2 -> "中度"
            else -> "重度"
        }

        // 根据疾病类型和严重程度生成治疗方案
        val treatmentPlan = when (diseaseType) {
            "支气管炎" -> "建议使用抗生素治疗，同时注意休息，保持室内通风。"
            "哮喘" -> "建议使用支气管扩张剂，避免接触过敏原，保持规律用药。"
            "肺炎" -> "建议住院治疗，使用抗生素，注意休息和营养。"
            "上呼吸道感染" -> "建议多休息，多喝水，必要时使用对症药物。"
            else -> "建议及时就医，进行专业诊断。"
        }

        // 生成生活建议
        val lifestyleAdvice = arrayListOf(
            "保持室内通风",
            "避免接触过敏原",
            "保持规律作息",
            "适当运动，增强体质"
        )

        // 生成饮食建议
        val dietaryAdvice = arrayListOf(
            "多喝水，保持水分",
            "避免辛辣刺激食物",
            "多吃新鲜蔬果",
            "注意营养均衡"
        )

        // 生成运动建议
        val exerciseAdvice = arrayListOf(
            "避免剧烈运动",
            "选择温和的有氧运动",
            "注意运动强度",
            "保持适度运动"
        )

        // 生成用药建议
        val medicationAdvice = arrayListOf(
            "遵医嘱用药",
            "按时服药",
            "注意药物副作用",
            "不要随意停药"
        )

        return RespiratoryDiagnosis(
            diseaseType = diseaseType,
            symptoms = selectedSymptoms,
            severity = severity,
            treatmentPlan = treatmentPlan,
            lifestyleAdvice = lifestyleAdvice,
            dietaryAdvice = dietaryAdvice,
            exerciseAdvice = exerciseAdvice,
            medicationAdvice = medicationAdvice
        )
    }

    private fun getCurrentUserId(): Int {
        // TODO: 实现获取当前用户ID的逻辑
        return 1
    }

    private fun saveDiagnosis(diagnosis: RespiratoryDiagnosis) {
        // TODO: 实现保存诊断结果的逻辑
    }

    private fun showDiagnosisResult(diagnosis: RespiratoryDiagnosis) {
        val intent = Intent(this, RespiratoryDiagnosisResultActivity::class.java).apply {
            putExtra("diseaseType", diagnosis.diseaseType)
            putExtra("symptoms", ArrayList(diagnosis.symptoms))
            putExtra("severity", diagnosis.severity)
            putExtra("treatmentPlan", diagnosis.treatmentPlan)
            putExtra("lifestyleAdvice", ArrayList(diagnosis.lifestyleAdvice))
            putExtra("dietaryAdvice", ArrayList(diagnosis.dietaryAdvice))
            putExtra("exerciseAdvice", ArrayList(diagnosis.exerciseAdvice))
            putExtra("medicationAdvice", ArrayList(diagnosis.medicationAdvice))
        }
        startActivity(intent)
        finish()
    }
} 